﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.Framework.Commons;
using IGM.Framework.Constants;

namespace IGM.Presentation.Assignment
{
    public partial class GroupLecturersControl : UserControl
    {
        private readonly Business.Business _business;
        private List<GroupLecturer> _groups;

        public GroupLecturersControl()
        {
            InitializeComponent();

            _business = Main.Main.Instance.IgmBusiness;
        }

        private void GroupLecturersControlLoad(object sender, EventArgs e)
        {
            LoadKindIntern();
            LoadMajor();
            LoadLecturer();  
            LoadGroup();
            Dock = DockStyle.Top;
        }

        private void LoadMajor()
        {
            cboMajors.DataSource = _business.GetListMajor();
            cboMajors.DisplayMember = PropertiesConstants.Name;
            cboMajors.ValueMember = PropertiesConstants.Id;
        }

        private void LoadKindIntern()
        {
            cboKindInterns.DataSource = _business.GetListKindIntern();
            cboKindInterns.DisplayMember = PropertiesConstants.Name;
            cboKindInterns.ValueMember = PropertiesConstants.Id;
        }

        private void LoadLecturer()
        {
            cboLecturers.DataSource = _business.GetListLecturerByMajorId(((Major)cboMajors.SelectedItem).Id);
            cboLecturers.DisplayMember = PropertiesConstants.Name;
            cboLecturers.ValueMember = PropertiesConstants.Id;
        }

        private void LoadGroup()
        {
            _groups = _business.GetGroupNeedLecturer(((Major)cboMajors.SelectedItem).Id, ((KindIntern)cboKindInterns.SelectedItem).Id);
            txtCountGroups.Text = _groups.Count.ToString();
            txtCountLecturers.Text = _business.GetListLecturerByMajorId(((Major) cboMajors.SelectedItem).Id).Count.ToString();
            LoadGroupLecturer();
        }


        private void LoadGroupLecturer()
        {
            if (cboMajors.SelectedItem == null) return;
            dgvGroupLecturer.DataSource = _business.GetGroupLecturerByMajorId(((Major)cboMajors.SelectedItem).Id, ((KindIntern)cboKindInterns.SelectedItem).Id);
            DataGridViewHelper.ShowHideColumns(dgvGroupLecturer,
                                               new[] { PropertiesConstants.IsChecked, "GroupName", "LecturerName", "Year" });
            DataGridViewHelper.ShowIsCheck(dgvGroupLecturer);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvGroupLecturer, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvGroupLecturer, new[] { "IsChecked", "GroupName", "LecturerName", "Year" },
                                                   new[] { "Chọn", "Tên nhóm", "Giảng viên hướng dẫn", "Năm" });           
            GroupLecturerCurrentRowChanged();
        }

        private void GroupLecturerCurrentRowChanged()
        {
            if (dgvGroupLecturer.CurrentRow != null)
            {
                txtGroup.Text = dgvGroupLecturer.CurrentRow.Cells["GroupName"].Value.ToString();
                cboLecturers.Text = dgvGroupLecturer.CurrentRow.Cells["LecturerName"].Value.ToString();
                txtYear.Text = dgvGroupLecturer.CurrentRow.Cells["Year"].Value.ToString();
            }

            else
            {
                txtGroup.Text = txtYear.Text = string.Empty;
                cboLecturers.SelectedIndex = -1;
            }
        }

        private void DgvGroupLecturerCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvGroupLecturer == null ||
               dgvGroupLecturer.Columns[(string)typeof(GroupLecturer).Properties().IsChecked] == null ||
               dgvGroupLecturer.Columns[(string)typeof(GroupLecturer).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvGroupLecturer[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvGroupLecturer[e.ColumnIndex, e.RowIndex].Value;
        }

        private void DgvGroupLecturerCurrentCellChanged(object sender, EventArgs e)
        {
            GroupLecturerCurrentRowChanged();
        }

        private void ReloadGroupLecturer(GroupLecturer grouplecturer)
        {
            LoadGroup();
            DataGridViewHelper.SetDgvCurrentCell(dgvGroupLecturer, grouplecturer.GroupName);
            GroupLecturerCurrentRowChanged();
        }

        private void BtnEditGroupLecturerClick(object sender, EventArgs e)
        {
            try
            {
                if (dgvGroupLecturer.CurrentRow != null)
                {
                    var grouplecturer = new GroupLecturer
                    {
                        Id = Convert.ToInt32(dgvGroupLecturer.CurrentRow.Cells[PropertiesConstants.Id].Value),
                        IdLecturer = Convert.ToInt32(((Lecturer)cboLecturers.SelectedItem).Id),
                        Year = Convert.ToInt32(dgvGroupLecturer.CurrentRow.Cells["Year"].Value)
                    };
                    string message;
                    if (_business.EditGroupLecturerValidator(grouplecturer, out message))
                    {
                        if (!_business.EditGroupLecturer(grouplecturer))
                        {
                            MessageBox.Show(ConstantMessages.SaveItemNotSuccess, FileConstants.ProgramName);
                            return;
                        }
                        ReloadGroupLecturer(grouplecturer);
                    }
                    else
                        MessageBox.Show(message);
                }
                else
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.ProgramName);
            }

            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        private void ComboBoxSelectionChangeCommitted(object sender, EventArgs e)
        {
            if (((KindIntern) cboKindInterns.SelectedItem).Id == 3)
            {
                //Delete old control
                Controls.Clear();

                // Add new control
                var studentLecturers = new StudentLecturersControl();
                Controls.Add(studentLecturers);
            }
            else
                LoadGroup();
        }     
    }
}
